package com.example.administrator.myapplication;

import android.content.Intent;
import android.os.Looper;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

public class LoginActivity extends AppCompatActivity {
    private static  String url="http://192.168.3.23:8080/SpringWebMvcTest/login?";
    private static  String url_constant="http://192.168.3.23:8070/SpringWebMvc/login?";
    private EditText userName,password;
    private Button btn_login,btn_regist;
    private String userNameValue,passwordValue;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
    }

    public void initView(){
        //设置背景透明
        View v = findViewById(R.id.activity_login);
        v.getBackground().setAlpha(200);
        //获得实例对象
        userName = (EditText) findViewById(R.id.et_zh);
        password = (EditText) findViewById(R.id.et_mima);
        btn_regist=(Button) findViewById(R.id.btn_regist);
        btn_regist.getBackground().setAlpha(100);
        btn_login = (Button) findViewById(R.id.btn_login);
        btn_login.getBackground().setAlpha(100); //设置透明度
    }

    public void login(View view) {
        final String userNameValue=userName.getText().toString();
        final String passwordValue=password.getText().toString();
        setContentView(R.layout.activity_login);
        new Thread(){
            @Override
            public void run() {
                loginByOkHttp(userNameValue,passwordValue);

            }
        }.start();

    }
    public void loginByOkHttp(String name,String password){
        OkHttpClient client=new OkHttpClient();
        url = url_constant+"username="+name+"&password="+password;
        Request request =new Request.Builder()
                .url(url)
                .build();
        try {
            Response resp = client.newCall(request).execute();
            Log.i("kun1",url);
            Log.i("isSuccess",resp.isSuccessful()+"");
            if(resp.isSuccessful()){
                //为主线程开启消息循环
                Looper.prepare();
                Toast.makeText(LoginActivity.this,"成功！！！",Toast.LENGTH_SHORT).show();
                Intent intent=new Intent(this,LogoActivity.class);
                LoginActivity.this.startActivity(intent);
                String u=resp.body().string();
                Log.i("kun2",u);
                //Looper.loop();
            }else{
                Log.i("..",".......");
                Toast.makeText(LoginActivity.this,"失败！！！",Toast.LENGTH_SHORT).show();

            }

        }catch (Exception e){
            Log.i("error",e.getLocalizedMessage());
        }


    }

    public void regist(View view) {
        Intent intent=new Intent(this,UserRegistActivity.class);
        LoginActivity.this.startActivity(intent);
    }
}
